package com.symaster.gameengine.core;

import org.lwjgl.Version;
import org.lwjgl.glfw.GLFWErrorCallback;
import org.lwjgl.opengl.GL;

import static org.lwjgl.glfw.Callbacks.glfwFreeCallbacks;
import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.system.MemoryUtil.NULL;

/**
 * 主要窗口
 *
 * @author yinmiao
 * @since 2024/9/7
 */
public class EngineFrame {

    /**
     * 宽高比(宽除高), 默认1.778f(1600x900)
     */
    private float aspectRatio;
    /**
     * 当前窗口
     */
    private long window;
    /**
     * 设定fps
     */
    private float targetFPS;
    /**
     * 设定canvas
     */
    private Canvas sceneCanvas;
    /**
     * 设定当前窗口标题
     */
    private String title;
    /**
     * 设定当前窗口宽度
     */
    private int windowWidth;
    /**
     * 设定当前窗口高度
     */
    private int windowHeight;

    public EngineFrame() {
        this.targetFPS = 120;
        this.aspectRatio = 1.778f;
        this.title = "GameEngine";
        this.windowWidth = 1024;
        this.windowHeight = 768;

        GLFWErrorCallback.createPrint(System.err).set();
        if (!glfwInit()) {
            throw new IllegalStateException("Unable to initialize GLFW");
        }

        // 配置 GLFW
        glfwDefaultWindowHints();
        glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
        glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE);
    }

    /**
     * 启动当前窗口
     */
    public void run() {
        if (sceneCanvas == null) {
            throw new IllegalStateException("canvas not set");
        }

        System.out.println("LWJGL version: " + Version.getVersion());
        createWindow();

        // 设置窗口上下文为当前线程
        glfwMakeContextCurrent(window);
        glfwSwapInterval(0); // 禁用 V-Sync
        glfwShowWindow(window);
        GL.createCapabilities(); // 初始化 OpenGL 上下文

        sceneCanvas.setEngineFrame(this);
        sceneCanvas.resourceLoad();
        sceneCanvas.loop(window, windowWidth, windowHeight);
        sceneCanvas.setEngineFrame(null);

        // loop();
        glfwFreeCallbacks(window);
        glfwDestroyWindow(window);
        glfwTerminate();

        try (GLFWErrorCallback glfwErrorCallback = glfwSetErrorCallback(null)) {
            if (glfwErrorCallback != null) {
                glfwErrorCallback.free();
            }
        }
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public int getWindowWidth() {
        return windowWidth;
    }

    public void setWindowWidth(int windowWidth) {
        this.windowWidth = windowWidth;
    }

    public int getWindowHeight() {
        return windowHeight;
    }

    public void setWindowHeight(int windowHeight) {
        this.windowHeight = windowHeight;
    }

    public float getAspectRatio() {
        return aspectRatio;
    }

    public void setAspectRatio(float aspectRatio) {
        this.aspectRatio = aspectRatio;
    }

    public long getWindow() {
        return window;
    }

    public Canvas getCanvas() {
        return sceneCanvas;
    }

    public void setCanvas(Canvas sceneCanvas) {
        this.sceneCanvas = sceneCanvas;
    }

    public float getTargetFPS() {
        return targetFPS;
    }

    public void setTargetFPS(float targetFPS) {
        this.targetFPS = targetFPS;
    }

    private void createWindow() {
        // 创建窗口
        window = glfwCreateWindow(windowWidth, windowHeight, title, NULL, NULL);
        if (window == NULL) {
            throw new RuntimeException("Failed to create the GLFW window");
        }
    }

    private void loop() {

    }

    private void drawTriangle() {
        glViewport(100, 100, 100, 100);

        // 绘制三角形
        glBegin(GL_TRIANGLES);
        glColor3f(1.0f, 0.0f, 0.0f); // 顶点1 (红色)
        glVertex2f(-0.5f, -0.5f);

        glColor3f(0.0f, 1.0f, 0.0f); // 顶点2 (绿色)
        glVertex2f(0.5f, -0.5f);

        glColor3f(0.0f, 0.0f, 1.0f); // 顶点3 (蓝色)
        glVertex2f(0.0f, 0.5f);
        glEnd();
    }

}
